package com.dianning.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.Company;
import com.dianning.bean.Config;
import com.dianning.bean.Money;
import com.dianning.bean.MoneyLog;
import com.dianning.bean.Profile;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.IPSeeker;
import com.dianning.comm.Util;
import com.dianning.exception.DianningException;
import com.dianning.service.ConfigService;
import com.dianning.service.MoneyService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.web.interceptor.RemoteAddrAware;
import com.dianning.web.ui.OptionsString;

public class Register extends BaseAction implements SessionAware, RemoteAddrAware{

	private static final Log logger = LogFactory.getLog(Register.class);

	private UserInfoService userInfoService;
	private MoneyService moneyService;
	private ConfigService configService;
	private IPSeeker ipSeeker;

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public MoneyService getMoneyService() {
		return moneyService;
	}

	public void setMoneyService(MoneyService moneyService) {
		this.moneyService = moneyService;
	}
	
	public ConfigService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	public IPSeeker getIpSeeker() {
		return ipSeeker;
	}

	public void setIpSeeker(IPSeeker ipSeeker) {
		this.ipSeeker = ipSeeker;
	}
	
	private String remoteAddr;
	private Map session;
	
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private List questionList = new ArrayList();

	public List getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List questionList) {
		this.questionList = questionList;
	}
	
	private void setQuestionListValues() {
		questionList.add(new OptionsString("我手机号码的后6位？", "我手机号码的后6位？"));
		questionList.add(new OptionsString("我母亲的生日？", "我母亲的生日？"));
		questionList.add(new OptionsString("我父亲的生日？", "我父亲的生日？"));
		questionList.add(new OptionsString("我最好朋友的生日？", "我最好朋友的生日？"));
		questionList.add(new OptionsString("我儿时居住地的地址？", "我儿时居住地的地址？"));
		questionList.add(new OptionsString("我小学校名全称？", "我小学校名全称？"));
		questionList.add(new OptionsString("我中学校名全称？", "我中学校名全称？"));
		questionList.add(new OptionsString("离我家最近的医院全称？", "离我家最近的医院全称？"));
		questionList.add(new OptionsString("离我家最近的公园全称？", "离我家最近的公园全称？"));
		questionList.add(new OptionsString("我的座右铭是？", "我的座右铭是？"));
		questionList.add(new OptionsString("我最喜爱的电影？", "我最喜爱的电影？"));
		questionList.add(new OptionsString("我最喜爱的歌曲？", "我最喜爱的歌曲？"));
		questionList.add(new OptionsString("我最喜爱的食物？", "我最喜爱的食物？"));
		questionList.add(new OptionsString("我最大的爱好？", "我最大的爱好？"));
		questionList.add(new OptionsString("我最喜欢的小说？", "我最喜欢的小说？"));
		questionList.add(new OptionsString("我最喜欢的运动队？", "我最喜欢的运动队？"));
	}
	
	
	private String username;
	private String password;
	private String passfirm;
	private String name;
	private String question;
	private String answer;
	private String verifycode;
	private long vid;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassfirm() {
		return passfirm;
	}

	public void setPassfirm(String passfirm) {
		this.passfirm = passfirm;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}

	public long getVid() {
		return vid;
	}

	public void setVid(long vid) {
		this.vid = vid;
	}
	
	private String message;
	

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}

	public String index(){
		Config config = this.getConfigService().findConfigByID("register");
		if(config.getConfContext().equalsIgnoreCase("close")){
			this.message = "本网站注册功能己关闭";
			return "close";
		}
		if(config.getConfContext().equalsIgnoreCase("area")){
			String country = this.getIpSeeker().getCountry(this.getRemoteAddr());
			if(StringUtils.indexOf(country, "辽宁")==-1){
				this.message = "本网站限制省外用户注册";
				return "close";
			}
		}
		setQuestionListValues();
		this.setAction("add");
        return INPUT;
	}
	
	public String add(){
		Config config = this.getConfigService().findConfigByID("register");
		if(config.getConfContext().equalsIgnoreCase("close")){
			this.message = "本网站注册功能己关闭";
			return "close";
		}
		if(config.getConfContext().equalsIgnoreCase("area")){
			String country = this.getIpSeeker().getCountry(this.getRemoteAddr());
			if(StringUtils.indexOf(country, "辽宁")==-1){
				this.message = "本网站限制省外用户注册";
				return "close";
			}
		}
		//验证码正确
		if(!getSessionAuthCode().equalsIgnoreCase(verifycode)){
			this.addActionError(this.getText("error.reg.auth"));
			this.getSession().put("code", "auto_error");
			setQuestionListValues();
	        return INPUT;
		}
		UserInfo userInfo = this.getUserInfoService().findUserInfoByUsername(this.username);
		if(userInfo != null){
			this.addActionError(this.getText("error.reg.user"));
			this.getSession().put("code", "user_error");
			setQuestionListValues();
	        return INPUT;
		}
		/**
		if (hasActionErrors()||hasFieldErrors()) {	
			setQuestionListValues();
	        return INPUT;
        }
        */
		userInfo = new UserInfo();
		userInfo.setUsername(username);
		userInfo.setName("");
		userInfo.setPassword(password);
		userInfo.setRepassword(Util.hash(password));
		userInfo.setQuestion(question);
		userInfo.setAnswer(answer);
		userInfo.setCredit(0);
		userInfo.setRegIP(this.getRemoteAddr());
		userInfo.setRegTime(new Date());
		userInfo.setLastLoginIP(this.getRemoteAddr());
		userInfo.setLastLoginTime(new Date());
		userInfo.setAvailable(1);
		userInfo.setUsertype(1);
		UserDetail userDetail = new UserDetail();
		userDetail.setNotenum(0);
		userDetail.setMobile("");
		userDetail.setTel("");
		userDetail.setQq("");
		userDetail.setEmail("");
		userDetail.setSex(0);
		Resume resume = new Resume();
		resume.setName("");
		resume.setMobile("");
		resume.setEmail("");
		resume.setSex(0);
		resume.setBirthyear(1980);
		resume.setBirthmonth(1);
		resume.setBirthday(1);
		resume.setEdugrade(0);
		resume.setWorkExperience("");
		resume.setStudyExperience("");
		Company company = new Company();
		company.setCompanyname("");
		company.setCompanyTrade(0);
		company.setCompanyType(0);
		company.setCompanySize(0);
		company.setLinkman("");
		company.setTel("");
		company.setEmail("");
		company.setAddress("");
		company.setRemark("");
		company.setLicensestatus(0);
		Money money = new Money();
		money.setUsername(username);
		money.setPoints(Constant.INIT_MONEY);
		money.setTotal(Constant.INIT_MONEY);
		Profile profile = new Profile();
		profile.setName("");
		profile.setArea("");
		profile.setCid(0);
		profile.setCcid(0);
		profile.setManage("");
		profile.setCompanyname("");
		profile.setLinkman("");
		profile.setLinktel("");
		profile.setAddress("");
		profile.setService("");
		profile.setAbout("");
		profile.setKey1("");
		profile.setValue1("");
		profile.setKey2("");
		profile.setValue2("");
		profile.setKey3("");
		profile.setValue3("");
		profile.setKey4("");
		profile.setValue4("");
		profile.setStatus(1);
		profile.setShowstatus(0);
		profile.setWebkey("");
		profile.setCcidList(new ArrayList());
		profile.setStyle(1);
		profile.setStatus(Constant.PROFILE_STATUS);
		
		try {
			this.getUserInfoService().createUserInfo(userInfo, userDetail, resume, company, money, profile);
			if(vid != 0){
				Money vm = this.getMoneyService().findMoneyByUid(this.vid);
				vm.setPoints(vm.getPoints() + Constant.INVITE_MONEY);
				vm.setTotal(vm.getTotal() + Constant.INVITE_MONEY);
				UserInfo vuser = this.getUserInfoService().findUserInfoByUid(this.vid);
				MoneyLog vml = new MoneyLog();
				vml.setUid(this.vid);
				vml.setUsername(vuser.getUsername());
				vml.setLogtype("in");
				vml.setPoints(Constant.INVITE_MONEY);
				vml.setExplain("邀请好友赠送积分");
				this.getMoneyService().updateMoney(vm, vml);
			}
			this.getSession().put("code", "user_create");
			return INPUT;
        } catch (DianningException e) {
            this.addActionError(this.getText("error.reg.createrror"));
            return ERROR;
        }
	}
	
	private String getSessionAuthCode() {
        String auth = (String) this.getSession().get("authCode");
        if(StringUtils.isBlank(auth)){
        	return "";
        }else{
        	return auth;
        }
    }


}
